﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Business.Domain.Application;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Utils;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Customers;
using Shop.Business.Domain.Orders.Logic;
using Shop.Web.AppCode;

namespace Shop.Web
{
    public partial class ProductView : ShopBasePage
    {
        [QueryParameter("id", IsRequired = false)]
        public int id;

        public string GroupName;

        protected void Page_Load(object sender, EventArgs e)
        {
            WebParameterAttribute.SetValues(this, Request);
            ProductGroup productGroup = Locator.GetService<IProductGroupManager>().Load(id);
            btnEdit.Visible = Locator.GetService<IContextObjects>().Get<CustomerHolder>() != null &&
            Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object.Role == CustomerRole.Admin;
            imgProduct.Attributes["class"] = "ui-corner-all productImage";
            var ImageDS = new List<string>();
            if (File.Exists(HttpContext.Current.Server.MapPath("images") + "\\Product\\" + string.Format("IMG{0}.jpg", productGroup.Id)))
            {
                imgProduct.Attributes["src"] = "~/images/Product/IMG" + productGroup.Id.ToString() + ".jpg";
//                if (File.Exists(HttpContext.Current.Server.MapPath("images") + "\\Product\\" + string.Format("IMG{0}.jpg", productGroup.Id)))
//                {
//                    imgProduct.Attributes["class"] = "wrap ui-corner-all";
//                    imgProduct.Attributes["longdesc"] = "images/Product/IMG" + productGroup.Id.ToString() + ".jpg";
//                }
                ImageDS.Add("images/Product/thumb_IMG" + productGroup.Id.ToString() + ".jpg");
            }
            else
            {
                imgProduct.Attributes["src"] = ShopBasePage.NoImage;
                
            }
//            PriceCalculator calculator = new PriceCalculator(productGroup);
//            ltPrice.Text = calculator.GetGroupPrice();
            if (productGroup.Products.Count > 0 && productGroup.Visible)
            {
                Dictionary<string, List<Product>> dictionary = new Dictionary<string, List<Product>>();
                foreach (Product product in productGroup.Products)
                {
                    if (!dictionary.Keys.Contains(product.BrandArt))
                        dictionary.Add(product.BrandArt, new List<Product>());
                    dictionary[product.BrandArt].Add(product);
                }
                rprBrandArt.DataSource = dictionary;
                rprBrandArt.DataBind();
                btnBuy.ProdId = productGroup.Products.First().Id;
            }
            else
            {
                phSorry.Visible = true;
                phProductList.Visible = false;
            }
            ltName.Text = productGroup.Name;
            GroupName = productGroup.Name;
            SetTitle(GroupName);
            ltBrand.Text = productGroup.BrandName;
            ltCat.Text = productGroup.Category.Name;
            ltDescription.Text = StringUtils.IsBlankOrNull(productGroup.Description) ? "Мы пока не в состоянии описать восторгов от этой вещички. Стараемся, пишем..." : StringUtils.TextileToHtml(productGroup.Description);
            if (File.Exists(HttpContext.Current.Server.MapPath("txt") + "/size/" + productGroup.Brand.Id.ToString() + ".txt"))
            {
                string s =
                    StringUtils.TextileToHtml(File.ReadAllText(HttpContext.Current.Server.MapPath("txt") + "/size/" + productGroup.Brand.Id.ToString() + ".txt"));
                lblTxtSize.Text = s;
            }
            else
                lblTxtSize.Text = "Отберем у Димона линейку и все померяем :)";
            string path = HttpContext.Current.Server.MapPath("images") + "\\Product\\F" + productGroup.Id + "\\";
            rprSubImage.Visible = false;
            if (Directory.Exists(path))
            {
                var ImageDSAll = Directory.GetFiles(path);
                foreach (string s in ImageDSAll)
                {
                    if (s.Contains("thumb_"))
                        ImageDS.Add(s.Remove(0, s.IndexOf("images")).Replace('\\', '/'));
                }
                if (ImageDS.Count > 1)
                {
                    rprSubImage.Visible = true;
                    rprSubImage.DataSource = ImageDS;
                    rprSubImage.DataBind();
                }
            }
            rpParametrs.DataSource = productGroup.Parameters;
            rpParametrs.DataBind();


        }

        protected void btnEdit_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("ProductDetails.aspx?action=edit&id="+id);
        }

        protected void rprProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

//            Literal ltProductPrice = (Literal) e.Item.FindControl("ltProductPrice");
//            
//            if (ltProductPrice != null)
//            {
//                
//                Product product = (Product)e.Item.DataItem;
//                ltProductPrice.Text = product.Price.ToString();
//                Literal ltQty = (Literal)e.Item.FindControl("ltQty");
//                if (ltQty != null)
//                {
//                    ltQty.Text = product.AvailableStr;
//                }
//            }
//            
        }
    }
}
